package com.sun.util.qrcode;

import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class QrCodeUtils {
	public static final String TYPE_PRINT = "print";
	private static final String CHARSET = "utf-8";

	// 二维码尺寸
	private static final Integer QRCODE_SIZE = 180;// 300dpi 打印机打印精度和扫描仪清晰度
													// 约等于118像素
	// LOGO宽度
	private static final int WIDTH = 60;
	// LOGO高度
	private static final int HEIGHT = 60;

	private static final String LOGOPATH = "logo-qrcode.png";

	/**
	 * @author SunAFei
	 * @param content 内容
	 * @return
	 * @throws Exception
	 */
	public static BufferedImage createImage(String content) throws Exception {
		return createImage(content, QRCODE_SIZE);
	}

	/**
	 * 生成二维码
	 * 
	 * @author SunAFei
	 * @param content 内容
	 * @param size 宽度
	 * @return
	 * @throws Exception
	 */
	public static BufferedImage createImage(String content, Integer size) throws Exception {
		if (size == null) {
			size = QRCODE_SIZE;
		}
		Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
		hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
		hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
		hints.put(EncodeHintType.MARGIN, 1);
		BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size, hints);
		int width = bitMatrix.getWidth();
		int height = bitMatrix.getHeight();
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		for (int x = 0; x < width; x++) {
			for (int y = 0; y < height; y++) {
				image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
			}
		}
		return image;
	}

	/**
	 * @author zhangxiuwei
	 * @param content 二维码内容
	 * @param logoPath logo地址
	 * @param size 二维码尺寸
	 * @return
	 * @throws Exception
	 */
	public static BufferedImage createImageInLogo(String content, String logoPath, Integer size) throws Exception {
		BufferedImage image = createImage(content, size);
		// 插入图片
		QrCodeUtils.insertImageLogo(image, logoPath, size);
		return image;
	}

	/**
	 * @author zhangxiuwei
	 * @param content 二维码内容
	 * @param logoPath logo地址
	 * @return
	 * @throws Exception
	 */
	public static BufferedImage createImageInLogo(String content, String logoPath) throws Exception {
		BufferedImage image = createImage(content, QRCODE_SIZE);
		// 插入图片
		QrCodeUtils.insertImageLogo(image, logoPath, QRCODE_SIZE);
		return image;
	}

	/**
	 * @author SunAFei
	 * @param content 二维码内容
	 * @return
	 * @throws Exception
	 */
	public static BufferedImage createImageInLogo(String content) throws Exception {
		return createImageInLogo(content, LOGOPATH);
	}

	/**
	 * 插入logo
	 * 
	 * @author zhangxiuwei
	 * @param source 二维码图片流
	 * @param logoPath logo地址
	 * @throws Exception
	 */
	private static void insertImageLogo(BufferedImage source, String logoPath, Integer size) throws Exception {
		File file = new File(logoPath);
		if (!file.exists()) {
			System.err.println("" + logoPath + "   该文件不存在！");
			return;
		}
		Image src = ImageIO.read(new File(logoPath));
		int width = src.getWidth(null);
		int height = src.getHeight(null);
		// 压缩LOGO
		if (width > WIDTH) {
			width = WIDTH;
		}
		if (height > HEIGHT) {
			height = HEIGHT;
		}
		Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
		BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics g = tag.getGraphics();
		g.drawImage(image, 0, 0, null); // 绘制缩小后的图
		g.dispose();
		src = image;
		// 插入LOGO
		Graphics2D graph = source.createGraphics();
		int x = (size - width) / 2;
		int y = (size - height) / 2;
		graph.drawImage(src, x, y, width, height, null);
		Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
		graph.setStroke(new BasicStroke(3f));
		graph.draw(shape);
		graph.dispose();
	}
}